package com.module.lemlin.view

import android.content.Context
import android.text.SpannableString
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import android.view.animation.Animation
import com.blankj.utilcode.util.ColorUtils
import com.module.lemlin.R
import kotlinx.android.synthetic.main.popup_dialog_hint.view.*
import razerdp.basepopup.BasePopupWindow
import razerdp.util.animation.AlphaConfig
import razerdp.util.animation.AnimationHelper

class DialogHintPopup(
    context: Context,
    val onConfirm: (view: View) -> Unit,
    val onCancel: (view: View) -> Unit
) : BasePopupWindow(context) {

    init {
        setOverlayNavigationBar(false)
    }

    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_dialog_hint)
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        contentView.tvDialogHintConfirm.setOnClickListener {
            this.onConfirm(it)
        }
        contentView.tvDialogHintCancel.setOnClickListener {
            this.onCancel(it)
        }
    }

    override fun onCreateShowAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withAlpha(AlphaConfig.IN)
            .toShow()
    }

    override fun onCreateDismissAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withAlpha(AlphaConfig.OUT)
            .toDismiss()
    }

    fun setTextTitle(text: String): DialogHintPopup {
        contentView.tvDialogHintTitle.text = text
        return this
    }

    fun setTextMessage(text: String): DialogHintPopup {
        contentView.tvDialogHintMsg.text = text
        return this
    }

    fun setTextUserAgree(spannable: SpannableString): DialogHintPopup {
        contentView.tvDialogHintMsg.text = spannable
        contentView.tvDialogHintMsg.movementMethod = LinkMovementMethod.getInstance()
        return this
    }

    fun setTextConfirm(text: String): DialogHintPopup {
        contentView.tvDialogHintConfirm.text = text
        return this
    }

    fun setTextCancel(text: String): DialogHintPopup {
        contentView.tvDialogHintCancel.text = text
        return this
    }

    fun isTextCancelGone(isGone: Boolean): DialogHintPopup {
        contentView.tvDialogHintCancel.visibility = if (isGone) View.GONE else View.VISIBLE
        contentView.viewDialogHintDivider.visibility = if (isGone) View.GONE else View.VISIBLE
        return this
    }

    fun setText(
        textTitle: String?, textMessage: String?,
        textConfirm: String?, textCancel: String?
    ): DialogHintPopup {
        contentView.tvDialogHintTitle.text = textTitle
        contentView.tvDialogHintMsg.text = textMessage
        contentView.tvDialogHintConfirm.text = textConfirm
        contentView.tvDialogHintCancel.text = textCancel
        return this
    }
}